var ctx=window.daijia.ctx;
var totalElements=window.daijia.totalElements;

$(document).ready(function(){

	//初始化分页
	
	initPagedata(totalElements);
	
	//点击搜索
	$(document).on('click','.somehref',function(){
		
		var day=$(this).attr("day-number");
		var driverId=$(this).parent().parent().children(":first").attr("id-number");

		var recordUrl='/chart/recordTime';
	    var startyear=$("#startyear").val();
	 	var startmonth=$("#startmonth").val();
		
		
		recordUrl+='?startyear='+startyear;
		recordUrl+='&startmonth='+startmonth;
		
		recordUrl+='&days='+day;
		recordUrl+='&id='+driverId;
	 
	    window.location=recordUrl;
	});
	
	$("#search_button").click(function(){
		
		var newTotalElements=getTotalElements();
		
		initPagedata(newTotalElements);
		$(".pageshow").each(function(index,element){
			$(element).find("li a").html("共 "+newTotalElements+" 条数据");
		});
		
	});
		

	
	//导出。。。。。。。。
	
	//导出数量
	$("#allAmount").click(function(){
		if($(this).is(':checked')){
			$("#amounts").attr("disabled",true);
		}
		else{
			$("#amounts").attr("disabled",false);
		}
	});
	
	//开始导出
	$("#export_button").click(function(){
		
		if(!$("#allAmount").is(':checked')){
			var amounts=$("#amounts").val();
			if(amounts==""){
				$.scojs_message("请输入导出数量", $.scojs_message.TYPE_ERROR);
				return false;
			}
			else if(amounts<=0){
				$.scojs_message("导出数量最少为1条", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}
		
		/*if(!$("#exportTime").is(':checked')){
			var DATE_FORMAT=/^[1-9]+\d{3}\-(?:0[1-9]|1[0-2]|[1-9]{1})$/;
			if(!DATE_FORMAT.test($("#startTime").val()) || !DATE_FORMAT.test($("#endTime").val())){
				   $.scojs_message("您输入的日期格式有误，正确格式应为：2012-01", $.scojs_message.TYPE_ERROR);
				   return false;
				}
		}
		
		if(!$("#exportName").is(':checked')){
			
			if($("#startTime").val()=="" || $.trim($("#startTime").val()).length==0){
				   $.scojs_message("姓名不能为空", $.scojs_message.TYPE_ERROR);
				   return false;
				}
		}*/
		
		
		window.location=ctx+"/chart/exportDriverTime?"+$("#ExportForm").serialize();
	});
	
});

function loadData(page){
	
	if($('#loadblacklayer').length==0){
		var dw = $(".table").width();
		var dh = $(".table").height();
		var backlayer ="<div id='loadblacklayer'><div style='margin-top:"+(dh/2)+"px;margin-left:"+(dw/2)+"px;'><img src='"+ctx+"/assets/css/images/loading.gif' /></div></div>"
		$(".table tbody").append(backlayer);
		$('#loadblacklayer').css({"z-index":"9","background":"#000","opacity":"0.5","position":'absolute',"top":0,"width":dw,"height":dh});
	}
	
	var off_set=(page-1)*20;
	var newTotalElements=0;
	$.ajax({
        url : ctx+'/chart/driverTimeData',
        type:'GET',
        async:true,
        dataType : 'json',
        data : {
        	"pager.offset":off_set,
        	"name":$("#name").val(),
        	"startyear":$("#startyear").val(),
        	"startmonth":$("#startmonth").val(),
        	"companyId":$("#companyId").val()
        },
        success : function(result){
        	var newcontent="";
        	console.log(result);
        	newcontent+="<thead>";
        	newcontent+="<tr style='font-size:14px;'>";
        	newcontent+="<th style='min-width:90px'>姓名(工号)</th>";
        	$(result.listDay).each(function(index,data){
        		newcontent+="<th>"+data+"</th>";
        	});
        	newcontent+="<th style='min-width:50px'>月计</th>"
        	newcontent+="</tr>";
        	newcontent+="</thead>";	
        	
        	if(result.dataList.totalElements>0){
        		newcontent+="<tbody style='display:none;'>";	
        		$(result.dataList.content).each(function(index,driver){
        			
        			newcontent+="<tr>";
        			newcontent+="<td class='some-td driverId' id-number="+driver.id+" style='text-align:left'>"+driver.realname+"("+driver.username+")"+"</td>";
        			
        			$(driver.dayWorkTime).each(function(i,day){
        				newcontent+="<td 'class=some-td workTime'>"+(day)+"</td>";	 
        			});
        			
        			newcontent+="<td 'class=some-td'>"+(driver.workTime)+"</td>";	 
        			newcontent+="</tr>";
        		});
            	newcontent+="</tbody>";	
        	}

        	$('#loadblacklayer').remove();
        	$(".table tbody").remove();
        	$(".table thead").remove();
        	$(".table").append(newcontent);
        	$(".table tbody").show(300);
    		newTotalElements=result.dataList.totalElements;
    		
        }
	});
	
	return newTotalElements;
}

//分页初始化
function initPagedata(total){
	$(".pagedata").paging(total, {
		format: '[< ncnnn >]',
		perpage: 20,
		onSelect: function (page) {
			loadData(page);
	    	return false;
		},
		onFormat: function (type) {
			switch (type) {
			case 'block': // n and c
				if (!this.active)
					return '';
				else if (this.value != this.page)
					return '<li><a href="#">' + this.value + '</a></li>';
				return '<li class="active"><a href="#">' + this.value + '</a></li>';
			case 'next': // >
				return '<li><a href="#">&rsaquo;</a><li>';
			case 'prev': // <
					return '<li><a href="#">&lsaquo;</a><li>';
			case 'first': // [
					return '<li><a href="#">&laquo;</a><li>';
			case 'last': // ]
					return '<li><a href="#">&raquo;</a></li>';
			}
		}
	});
}

function getTotalElements(){
	
	$.ajax({
        url : ctx+'/chart/countDriverTime',
        type:'GET',
        async:false,
        dataType : 'json',
        data : {
        	"companyId":$("#companyId").val(),
        	"startyear":$("#startyear").val(),
        	"startmonth":$("#startmonth").val(),
        	"name":$("#name").val()
        },
        success : function(result){
        	newTotalElements=result;
        }
	});
	
	return newTotalElements;
}
	

